package org.leo.dm.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.codec.binary.Base64;

/**
 * 
 * Created on 2016年2月17日
 * <p>
 * Description base64压缩工具类
 * <p>
 * Copyright neusoft (c) 2015 .
 * 
 * @author xiao_h
 *
 */
public class ZipBase64Util {
	public InputStream decode(String data) {

		GZIPInputStream gin = null;
		try {
			gin = new GZIPInputStream(new ByteArrayInputStream(Base64.decodeBase64(data)), 1024);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				if (gin != null) {
					gin.close();
				}
			} catch (IOException e1) {
				e1.printStackTrace();
				gin = null;
			}
		}

		return gin;
	}

	public boolean decodeF(String sourcePath, String destPath) {
		boolean re = false;

		FileOutputStream out = null;
		GZIPInputStream gin = null;
		try {
			out = new FileOutputStream(new File(destPath));
			gin = new GZIPInputStream(new ByteArrayInputStream(Base64.decodeBase64(FileUtil.readToString(sourcePath))), 1024);

			int i = -1;
			byte[] buffer = new byte[1024];
			while ((i = gin.read(buffer, 0, 1024)) != -1) {
				out.write(buffer, 0, i);
			}
			gin.close();

			out.flush();
			out.close();
			re = true;
		} catch (Exception e) {
			e.printStackTrace();
			try {
				gin.close();
				out.flush();
				out.close();
			} catch (IOException e1) {
				e1.printStackTrace();
				gin = null;
				out = null;
				out = null;
			}
		}

		return re;
	}

	public boolean encodeF(String sourcePath, String destPath) {
		boolean re = false;

		OutputStream outStream = null;
		FileOutputStream out = null;
		ByteArrayOutputStream byteArrayOutputStream = null;
		GZIPOutputStream gzipos = null;
		byte[] bytes = null;

		FileInputStream fileInputStream = null;
		try {
			byteArrayOutputStream = new ByteArrayOutputStream();
			gzipos = new GZIPOutputStream(byteArrayOutputStream);
			out = new FileOutputStream(new File(destPath));

			fileInputStream = new FileInputStream(sourcePath);
			int i = -1;
			byte[] buffer = new byte[1024];
			while ((i = fileInputStream.read(buffer, 0, 1024)) != -1) {
				gzipos.write(buffer, 0, i);
			}

			gzipos.finish();
			gzipos.flush();
			gzipos.close();
			bytes = byteArrayOutputStream.toByteArray();

			String base64Str = Base64.encodeBase64String(bytes);

			out.write(base64Str.getBytes("UTF-8"));
			out.flush();
			re = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				gzipos = null;
				byteArrayOutputStream.close();
				byteArrayOutputStream = null;
				if (out != null) {
					out.close();
					out = null;
				}
				if (outStream != null) {
					outStream.close();
					outStream = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return re;
	}

	public String encodeF(String sourcePath) {
		OutputStream outStream = null;
		ByteArrayOutputStream byteArrayOutputStream = null;
		GZIPOutputStream gzipos = null;
		byte[] bytes = null;
		String base64Str = null;

		FileInputStream fileInputStream = null;
		try {
			byteArrayOutputStream = new ByteArrayOutputStream();
			gzipos = new GZIPOutputStream(byteArrayOutputStream);

			fileInputStream = new FileInputStream(sourcePath);
			int i = -1;
			byte[] buffer = new byte[1024];
			while ((i = fileInputStream.read(buffer, 0, 1024)) != -1) {
				gzipos.write(buffer, 0, i);
			}

			gzipos.finish();
			gzipos.flush();
			gzipos.close();
			bytes = byteArrayOutputStream.toByteArray();
			base64Str = Base64.encodeBase64String(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				gzipos = null;
				byteArrayOutputStream.close();
				byteArrayOutputStream = null;

				if (outStream != null) {
					outStream.close();
					outStream = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return base64Str;
	}

	public String encode(String str) {
		OutputStream outStream = null;
		ByteArrayOutputStream byteArrayOutputStream = null;
		GZIPOutputStream gzipos = null;
		byte[] bytes = null;
		String base64Str = null;
		try {
			byteArrayOutputStream = new ByteArrayOutputStream();
			gzipos = new GZIPOutputStream(byteArrayOutputStream);
			gzipos.write(str.getBytes("UTF-8"));
			gzipos.finish();
			gzipos.flush();
			gzipos.close();
			bytes = byteArrayOutputStream.toByteArray();
			base64Str = Base64.encodeBase64String(bytes);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				gzipos = null;
				byteArrayOutputStream.close();
				byteArrayOutputStream = null;

				if (outStream != null) {
					outStream.close();
					outStream = null;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return base64Str;
	}

	public static void main(String[] args) {
		new ZipBase64Util().encodeF("d:/shk_1116_1655using.xml", "d:/shk_1116_1655z.xml");
		new ZipBase64Util().decodeF("d:/shk_1116_1655z.xml", "d:/ssssssssssssssssssssss.xml");
		
	}
}
